/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAllowedLayerForm.java
 *
 * Created on May 25, 2011, 10:30:13 AM
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.ControllerException;
import com.iceberg.adgt.common.DomainException;
import com.iceberg.adgt.common.Observer;
import com.iceberg.adgt.common.ObserverInterest;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.domain.LayerDTO;
import com.iceberg.adgt.ui.architecture.ArchitectureController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author David
 */
public class AddAllowedLayerForm extends javax.swing.JPanel implements Observer {

	/**
	 * Creates new form AddAllowedLayerForm
	 */
	public AddAllowedLayerForm() {
		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		addLayerLabel = new javax.swing.JLabel();
		addAllowedLayerBox = new javax.swing.JComboBox();
		addLayerButton = new javax.swing.JButton();

		addLayerLabel.setText("Layer:");

		addAllowedLayerBox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));

		addLayerButton.setText("Add layer");
		addLayerButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addLayerButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(addLayerLabel)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(addAllowedLayerBox, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(addLayerButton))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(addLayerLabel)
								.addComponent(addLayerButton)
								.addComponent(addAllowedLayerBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		);
	}// </editor-fold>//GEN-END:initComponents

	private void addLayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLayerButtonActionPerformed
		try {
			controller.addAllowedLayerToLayer((String) this.addAllowedLayerBox.getSelectedItem());
		} catch (DomainException ex) {
			LOG.error("Unable to add allowed layer to another layer", ex);
		} catch (ControllerException ex) {
			LOG.error("No selection is made", ex);
		}
	}//GEN-LAST:event_addLayerButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JComboBox addAllowedLayerBox;
	private javax.swing.JButton addLayerButton;
	private javax.swing.JLabel addLayerLabel;
	// End of variables declaration//GEN-END:variables
	private static final Logger LOG = LoggerFactory.getLogger(AddAllowedLayerForm.class);
	private ArchitectureController controller;


	@Override
	public final void notifyChange(ObserverInterest interest) {
		this.setAllowedLayerBox();
	}

	final void setAllowedLayerBox() {
		if (this.controller != null) {
			try {
				this.addAllowedLayerBox.removeAllItems();

				Map<String, LayerDTO> availableAllowedLayers = this.controller.getAvailableAllowedLayers();

				for (LayerDTO layerDTO : availableAllowedLayers.values()) {
					this.addAllowedLayerBox.addItem(layerDTO.getName());
				}

				if (this.addAllowedLayerBox.getItemCount() > 0) {
					this.addAllowedLayerBox.setEnabled(true);
					this.addLayerButton.setEnabled(true);
				} else {
					this.addAllowedLayerBox.setEnabled(false);
					this.addLayerButton.setEnabled(false);
				}
			} catch (ControllerException ex) {
				this.addAllowedLayerBox.setEnabled(false);
			}
		}
	}

	private void init() {
		DomainFacade facade = DomainFacadeImpl.getInstance();
		facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
		this.addAllowedLayerBox.setEnabled(false);
		this.addLayerButton.setEnabled(false);
	}

	public final void setController(ArchitectureController controller) {
		this.controller = controller;
		this.controller.addObserver(this, ObserverInterest.LAYER_SELECTION_CHANGED);
	}
}
